package com.example.prefsaver;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class PrefSaverActivity extends Activity {
	
	public static final String PREFS_NAME = "myPrefsFile";
	
	//GUI
	TextView textview;
	EditText edit_name;
	EditText edit_value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pref_saver);
		
		textview = (TextView) findViewById(R.id.textView_prefs);
		edit_name = (EditText) findViewById(R.id.editText_name_input);
		edit_value = (EditText) findViewById(R.id.editText_value_input);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pref_saver, menu);
		return true;
	}
	
	public void savePref(View view){
		if ((edit_name.getText().toString().length()>0)&&(edit_value.getText().toString().length()>0)){
			SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
			SharedPreferences.Editor editor = prefs.edit();
			
			editor.putString(edit_name.getText().toString(), edit_value.getText().toString());
			editor.commit();
			
			edit_name.setText("");
			edit_value.setText("");
		}
	}
	
	public void showPrefs(View view){
		SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
		
		String text = "";
		
		for (String key : prefs.getAll().keySet()){
			String str = key + ":  " + prefs.getAll().get(key) + "\n";
			text += str;
		}
		
		textview.setText(text);
	}
	
	public void resetPrefs(View view){
		SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = prefs.edit();
		
		editor.clear();
		editor.commit();
	}
}
